<template>
  <div>
    <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable :close-on-click-modal="false"
      :close-on-press-escape="false" width="569px">
      <el-form ref="formRef" :model="form" size="default" label-width="80px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="设备名称" prop="deviceName"
              :rules="[{ required: false, message: '请输入设备名称', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.deviceName" placeholder="请输入设备名称" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="制造商" prop="manufacturer"
              :rules="[{ required: false, message: '请输入制造商', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.manufacturer" placeholder="请输入制造商" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="PLC型号" prop="model"
              :rules="[{ required: true, message: '请输入PLC型号', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.model" placeholder="请输入PLC型号" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="序列号" prop="serialNumber"
              :rules="[{ required: false, message: '请输入序列号', trigger: ['blur', 'change'] }]">
              <el-input type="number" v-model="form.serialNumber" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="固件版本" prop="firmwareVersion"
              :rules="[{ required: false, message: '请输入固件版本', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.firmwareVersion" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="硬件版本" prop="hardwareVersion"
              :rules="[{ required: false, message: '请输入硬件版本', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.hardwareVersion" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="排序" prop="sort"
              :rules="[{ required: false, message: '排序', trigger: ['blur', 'change'] }]">
              <el-input-number v-model="form.sort" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts" name="industry/plc-device/form">
import { reactive, toRefs, getCurrentInstance, ref } from 'vue'
import { PLCDeviceGetListOutput, PLCDeviceAddInput, PLCDeviceUpdateInput } from '/@/api/industry/data-contracts'
import { PlcDeviceApi } from '/@/api/industry/PlcDevice'
import eventBus from '/@/utils/mitt'

defineProps({
  title: {
    type: String,
    default: '',
  },
})
const { proxy } = getCurrentInstance() as any
const formRef = ref()
const state = reactive({
  showDialog: false,
  sureLoading: false,
  ProductionLineData: [] as PLCDeviceGetListOutput[],
  form: {} as PLCDeviceAddInput & PLCDeviceUpdateInput,
})
const { form } = toRefs(state)

// 打开对话框
const open = async (row: any = {}) => {
  if (row.id > 0) {
    const res = await new PlcDeviceApi().get({ id: row.id }, { loading: true }).catch(() => {
      proxy.$modal.closeLoading()
    })

    if (res?.success) {
      state.form = res.data as PLCDeviceAddInput & PLCDeviceUpdateInput
    }
  } else {
    state.form = {} as PLCDeviceAddInput & PLCDeviceUpdateInput
  }
  state.showDialog = true
}
// 确定
const onSure = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (!valid) return

    state.sureLoading = true
    let res = {} as any
    if (state.form.id != undefined && state.form.id > 0) {
      res = await new PlcDeviceApi().update(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    } else {
      res = await new PlcDeviceApi().add(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    }
    state.sureLoading = false

    if (res?.success) {
      eventBus.emit('refreshPLCDevice')
      state.showDialog = false
    }
  })
}

// 取消
const onCancel = () => {
  state.showDialog = false
}
defineExpose({
  open,
})
</script>